package com.example.gybicycle.service;

import java.util.Date;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.sql.CrudService;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.entity.SysRole;
import com.example.gybicycle.dao.SysRoleDao;
import com.example.gybicycle.entity.SysUser;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysRoleService extends CrudService<SysRoleDao, SysRole> {
    @Override
    public SysRole get(String id) {
        SysRole table=super.get(id);
        if(table!=null)
            table.setMenuList(dao.findMenus(id));

        return table;
    }


    @Override
    public int insert(SysRole entity) {
        entity.setId(NumberUtil.getNum(NumType.ROLE));
        int num=super.insert(entity);
        if(num>0)
            saveMenu(entity);
        return  num;
    }

    @Override
    public int update(SysRole entity) {
        int num=super.update(entity);
        if(num>0)
            saveMenu(entity);
        return num;
    }

    @Override
    public int insertByList(List<SysRole> list) {
        for(SysRole entity:list){
            entity.setId(NumberUtil.getNum(NumType.ROLE));
            saveMenu(entity);
        }
        return super.insertByList(list);
    }

    public List<SysRole> findList(SysRole entity){
        List<SysRole> list=super.findList(entity);
        return list;
    }

    @Override
    public int delete(Object id) {
        int num=super.delete(id);
        if(num>0)
            dao.delMenus(id.toString());
        return num;
    }

    public void saveMenu(SysRole entity){
        dao.delMenus(entity.getId());
        if(entity.getMenuList()==null||entity.getMenuList().size()==0)
            return;
        dao.saveMenus(entity.getId(),entity.getMenuList());
    }


}